package team.paimengshop.server_socket;

import org.json.simple.JSONObject;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;

import team.paimengshop.show.*;

public class ReceiveFromClient
{
    private Socket socket;
    private static SendToClient send;
    private ObjectInputStream ois;

    public void setSocket(Socket socket)
    {
        this.socket = socket;
    }

    public Socket getSocket()
    {
        return socket;
    }

    public static void setSend(SendToClient send)
    {
        ReceiveFromClient.send = send;
    }

    public static SendToClient getSend()
    {
        return send;
    }

    public ReceiveFromClient()
    {

    }

    public ReceiveFromClient(Socket socket, SendToClient send)
    {
        this.socket = socket;
        ReceiveFromClient.send = send;
        try
        {
            ois = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void ReadJsonObject(JSONObject jsonObject, SendToClient send)
    {
        if(jsonObject.containsKey("ask"))
        {
            if(jsonObject.get("ask").toString().equals("ask for refreshing userInfo"))
            {
                send.Send(ActionControl_Send.SendUserInfo(jsonObject));
            }
            else if(jsonObject.get("ask").toString().equals("ask for refreshing commodityInfo"))
            {
                send.Send(ActionControl_Send.SendCommodityInfo());
            }
            else if(jsonObject.get("ask").toString().equals("ask for refreshing orderInfo"))
            {
                send.Send(ActionControl_Send.SendOrderInfo(jsonObject));
            }
        }
        else if(jsonObject.containsKey("update cart and collection"))
        {
            if(jsonObject.get("update cart and collection").toString().equals("update cart"))
            {
                ActionControl_Receive.UpdateCart(jsonObject);
            }
            else if(jsonObject.get("update cart and collection").toString().equals("update collection"))
            {
                ActionControl_Receive.UpdateCollection(jsonObject);
            }
        }
        else if(jsonObject.containsKey("communication"))
        {
            if(jsonObject.containsKey("message"))
            {
                int userId = Integer.parseInt(jsonObject.get("userId").toString());
                String message = ActionControl_Receive.Communication_Message(jsonObject);

                ActionControl_Receive.getCustomerServiceforServerController().ShowChatMessage(userId, message);
            }
            else if(jsonObject.containsKey("image"))
            {
                int userId = Integer.parseInt(jsonObject.get("userId").toString());
                File image = ActionControl_Receive.Communication_Image(jsonObject);

                ActionControl_Receive.getCustomerServiceforServerController().ShowChatImage(userId, image);
            }
        }
        else if(jsonObject.containsKey("login and register"))
        {
            if(jsonObject.get("login and register").toString().equals("login"))
            {
                send.Send(ActionControl_Receive.Login(jsonObject));
            }
            else if(jsonObject.get("login and register").toString().equals("register"))
            {
                send.Send(ActionControl_Receive.Register(jsonObject));
            }
        }
        else if(jsonObject.containsKey("create order"))
        {
            ActionControl_Receive.CreateOrder(jsonObject);
        }
        else if(jsonObject.containsKey("change userInfo"))
        {
            if(jsonObject.get("change userInfo").toString().equals("change userName"))
            {
                ActionControl_Receive.ChangeUserName(jsonObject);
            }
            else if(jsonObject.get("change userInfo").toString().equals("change password"))
            {
                send.Send(ActionControl_Receive.ChangePassword(jsonObject));
            }
            else if(jsonObject.get("change userInfo").toString().equals("change address"))
            {
                ActionControl_Receive.ChangeAddress(jsonObject);
            }
            else if(jsonObject.get("change userInfo").toString().equals("change headImageURL"))
            {
                ActionControl_Receive.ChangeHeadImageURL(jsonObject);
            }
        }
        else
        {
            System.out.println("json出错！");
        }
    }

    public void Receive()
    {
        try
        {
            while (ois != null)
            {
                try
                {
                    JSONObject jsonObject = (JSONObject) ois.readObject();
                    ReadJsonObject(jsonObject, send);
                } catch (EOFException e)
                {
                    e.printStackTrace();
                }
            }
        }
        catch (SocketException e)
        {
            System.out.println("有客户端断开了");
            ois = null;
            ServerManage.setClientNumber(ServerManage.getClientNumber() - 1);
        }
        catch (IOException | ClassNotFoundException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                socket.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

}
